#include "PlayerController.h"

#define PI 3.141592f
#define SPEED 0.1f

#include "InputMgr.h"
#include "MatrixStack.h"
#include "Network.h"
#include "SharedFunctions.h"

void PlayerController::Update(float deltaT)
{
	D3DXMATRIX rotateMatrix;
	D3DXVECTOR3 positionStore;
	D3DXMatrixRotationY( &rotateMatrix, ( (PI * (InputMgr::Get()->GetMouseX() / 700.f)) * deltaT ) );
	D3DXMATRIX playerMatrix = m_Player->m_data.data.Matrix;
	positionStore.x = playerMatrix.m[3][0];
	positionStore.y = playerMatrix.m[3][1];
	positionStore.z = playerMatrix.m[3][2];
	D3DXMatrixMultiply(&playerMatrix, &playerMatrix, &rotateMatrix);
	playerMatrix.m[3][0] = positionStore.x;
	playerMatrix.m[3][1] = positionStore.y;
	playerMatrix.m[3][2] = positionStore.z;

	m_Player->m_data.data.Matrix = playerMatrix;

	if( InputMgr::Get()->GetKeyState(KEY_W) )
	{
		D3DXVECTOR3 deltaP( 0.0f, 0.0f, SPEED);
		deltaP = VectorMatrixMultiply(playerMatrix, deltaP);
		m_Player->m_data.data.Matrix.m[3][0] += deltaP.x;
		m_Player->m_data.data.Matrix.m[3][2] += deltaP.z;
	}
	if( InputMgr::Get()->GetKeyState(KEY_S) )
	{
		D3DXVECTOR3 deltaP( 0.0f, 0.0f, -SPEED);
		deltaP = VectorMatrixMultiply(playerMatrix, deltaP);
		m_Player->m_data.data.Matrix.m[3][0] += deltaP.x;
		m_Player->m_data.data.Matrix.m[3][2] += deltaP.z;
	}
	if( InputMgr::Get()->GetKeyState(KEY_A) )
	{
		D3DXVECTOR3 deltaP( SPEED, 0.0f, 0.0f);
		deltaP = VectorMatrixMultiply(playerMatrix, deltaP);
		m_Player->m_data.data.Matrix.m[3][0] += deltaP.x;
		m_Player->m_data.data.Matrix.m[3][2] += deltaP.z;
	}
	if( InputMgr::Get()->GetKeyState(KEY_D) )
	{
		D3DXVECTOR3 deltaP( -SPEED, 0.0f, 0.0f);
		deltaP = VectorMatrixMultiply(playerMatrix, deltaP);
		m_Player->m_data.data.Matrix.m[3][0] += deltaP.x;
		m_Player->m_data.data.Matrix.m[3][2] += deltaP.z;
	}
	if( InputMgr::Get()->GetMouseDown() )
	{
		char data[MAX_MESSAGE_SIZE];
		data << *m_Player;

		Network::Get()->SendShot( data, m_Player->GetSize(), m_Player->m_controller->GetRef() );
	}

	//Set the eye position to the position of the player
	D3DXVECTOR3 EyePt(m_Player->m_data.data.Matrix.m[3][0], m_Player->m_data.data.Matrix.m[3][1], m_Player->m_data.data.Matrix.m[3][2]);
	//Move the eye to behind the player
	D3DXVECTOR3 cameraDelta(0.f, 0.f, -6.5f);
	cameraDelta = VectorMatrixMultiply(playerMatrix, cameraDelta);
	EyePt.x += cameraDelta.x;
	EyePt.y += cameraDelta.y;
	EyePt.z += cameraDelta.z;
	EyePt.y += 2.5f;

	//Look ahead of the player
	D3DXVECTOR3 LookatPt( 0.0f, 0.0f, 1.0f);
	LookatPt = VectorMatrixMultiply(playerMatrix, LookatPt);
	LookatPt.x += playerMatrix.m[3][0];
	LookatPt.y += playerMatrix.m[3][1];
	LookatPt.z += playerMatrix.m[3][2];

	//Define up as being above the player
    D3DXVECTOR3 UpVec( 0.0f, 1.0f, 0.0f );

	//Create the view matrix
    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH( &matView, &EyePt, &LookatPt, &UpVec );

    //Set the Matrix Stack, View
	MatrixStack::ReturnInstance()->SetViewMatrix(matView);

	//Test Matrix
	//D3DXVECTOR3 EyePta(5.f, 0.f, 5.f);
	//D3DXVECTOR3 LookatPta( 0.0f, 0.0f, 0.0f);
    //D3DXVECTOR3 UpVeca( 0.0f, 1.0f, 0.0f );
    //D3DXMATRIXA16 matViewa;
    //D3DXMatrixLookAtLH( &matViewa, &EyePta, &LookatPta, &UpVeca );
	//MatrixStack::ReturnInstance()->SetViewMatrix(matViewa);
}